package com.fyyg.service;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.fyyg.common.order.OrderPayEnum;
import com.fyyg.common.order.OrderStateEnum;
import com.fyyg.common.util.date.DateUtil;
import com.fyyg.dal.TBusiOrderDetailMapper;
import com.fyyg.dal.TBusiOrderMapper;
import com.fyyg.dal.TBusiPackageMapper;
import com.fyyg.dal.TBusiProductMapper;
import com.fyyg.interfaces.TBusiOrderService;
import com.fyyg.model.ApplyOrder;
import com.fyyg.model.TBusiOrder;
import com.fyyg.model.TBusiOrderDetail;
import com.fyyg.model.TBusiPackage;
import com.fyyg.model.TBusiProduct;



@Service("TBusiOrderService")
public class TBusiOrderServiceImpl implements TBusiOrderService {

	@Resource private TBusiOrderMapper tBusiOrderMapper;
	
	@Resource private TBusiOrderDetailMapper tBusiOrderDetailMapper;
	
	@Resource private TBusiPackageMapper tBusiPackageMapper;
	
	@Resource private TBusiProductMapper TBusiProductMapper;
	
	@Override
	public int applyOrder(ApplyOrder applyOrder) {
		TBusiOrder tBusiOrder = new TBusiOrder();
		BeanUtils.copyProperties(applyOrder, tBusiOrder);
		tBusiOrder.setPayType(OrderPayEnum.Order_Pay_Dind.getCode());
		tBusiOrder.setOrderStatus(OrderStateEnum.Order_State_Apply.getCode());
		Date currentDateTime = DateUtil.getCurrentDateTime();
		tBusiOrder.setOrderTime(currentDateTime);
		String operator= "activity";
		tBusiOrder.setCreatedTime(currentDateTime);
		tBusiOrder.setUpdatedTime(currentDateTime);
		tBusiOrder.setCreatedBy(operator);
		tBusiOrder.setUpdatedBy(operator);
		
		boolean bl = false;
		TBusiPackage tBusiPackage = tBusiPackageMapper.selectByPrimaryKey(applyOrder.getProPkgId());
		if(null!=tBusiPackage) {
			tBusiOrder.setProPkgId(tBusiPackage.getPkgId());
			TBusiProduct  tBusiProduct= TBusiProductMapper.selectByProId(tBusiPackage.getProId());
			if(null != tBusiProduct) {
				tBusiOrderMapper.insert(tBusiOrder);
				
				TBusiOrderDetail tBusiOrderDetail = new TBusiOrderDetail();
				tBusiOrderDetail.setOrderId(tBusiOrder.getOrderId());
				tBusiOrderDetail.setProId(tBusiProduct.getProId());
				tBusiOrderDetail.setProColor(tBusiProduct.getProColor());
				tBusiOrderDetail.setProSize(tBusiProduct.getProSize());
				tBusiOrderDetail.setPkgId(tBusiPackage.getPkgId());
				tBusiOrderDetail.setCreatedTime(currentDateTime);
				tBusiOrderDetail.setUpdatedTime(currentDateTime);
				tBusiOrderDetail.setCreatedBy(operator);
				tBusiOrderDetail.setUpdatedBy(operator);
				int i = tBusiOrderDetailMapper.insert(tBusiOrderDetail);
				
				bl = i==1?true:false;
			}
		}else {
			//TODO 提示套餐不存在
		}
		
		return bl?1:0;
	}

}
